package com.mobzilla.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mobzilla.model.Bug;
import com.mobzilla.model.User;
import com.mobzilla.service.ServiceFactory;
import com.mobzilla.util.Constants;

public final class ReportBugActivity extends Activity implements OnClickListener
{
	public void onCreate(Bundle b)
	{
		super.onCreate(b);
		
		setContentView(R.layout.addbug);
		
		initListeners();
	}
	
	private void initListeners()
	{
		Button addButton = (Button)findViewById(R.id.addBugButton);
		
		addButton.setOnClickListener(this);
		
		Button backButton = (Button)findViewById(R.id.addBackButton);
		
		backButton.setOnClickListener(new OnClickListener()
		{	
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
	}
	
	public void onClick(View v)
	{
		Bug bug = screenScrape();
		
		if(!ServiceFactory.getCreateServiceInstance().createNewDefect(bug, 
						Constants.getCurrentUser().getUsername(), 
						Constants.getCurrentUser().getPassword()))
		{
			Toast.makeText(this, "Unable to report bug.", Toast.LENGTH_LONG).show();
		}
		else
		{
			Toast.makeText(this, "Request sent to server successfully", Toast.LENGTH_LONG).show();
		}
	}
	
	private Bug screenScrape()
	{
		Bug b = new Bug();
		
		User creator = new User();
		creator.setName(Constants.getCurrentUser().getUsername());
		creator.setRealName(Constants.getCurrentUser().getUsername());
		
		b.setCreator(creator);
		
		setPriority(b);
		
		setSummary(b);
		
		setPlatform(b);
		
		setOperatingSystem(b);

		setSeverity(b);
		
		setVersion(b);
		
		setComponent(b);
		
		setProduct(b);
		
		setStatus(b);
		
		return b;
	}
	
	private void setOperatingSystem(Bug b)
	{
		Spinner sp = (Spinner)findViewById(R.id.addplatform);
		
		String selected = sp.getSelectedItem().toString();
		
		b.setOpSys(selected);
	}
	
	private void setStatus(Bug b)
	{
		Spinner sp = (Spinner)findViewById(R.id.addstatus);
		
		String selected = sp.getSelectedItem().toString();
		
		b.setStatus(selected);
	}
	
	private void setProduct(Bug b)
	{
		TextView tv = (TextView)findViewById(R.id.addProductTextField);
		
		String summary = tv.getText().toString();
		
		b.setProduct(summary);
	}
	
	private void setComponent(Bug b)
	{
		Spinner sp = (Spinner)findViewById(R.id.addComponentSpinner);
		
		String selected = sp.getSelectedItem().toString();
		
		b.setComponent(selected);
	}
	
	private void setVersion(Bug b)
	{
		TextView tv = (TextView)findViewById(R.id.addVersionTextField);
		
		String summary = tv.getText().toString();
		
		b.setVersion(summary);
	}
	
	private void setSeverity(Bug b)
	{
		Spinner sp = (Spinner)findViewById(R.id.addseverity);
		
		String selected = sp.getSelectedItem().toString();
		
		b.setSeverity(selected);
	}
	
	private void setPlatform(Bug b)
	{
		b.setPlatform("PC");
	}
	
	private void setSummary(Bug b)
	{
		TextView tv = (TextView)findViewById(R.id.addSummaryTextField);
		
		String summary = tv.getText().toString();
		
		b.setSummary(summary);
	}
	
	private void setPriority(Bug b)
	{
		Spinner sp = (Spinner)findViewById(R.id.addpriority);
		
		String selected = sp.getSelectedItem().toString();
		
		b.setPriority(selected);
	}
}
